package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;

import primetest.impl.manager.SessionManager;

@Named("mainMenuControler")
@SessionScoped
public class MainMenuControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -4614044005512888077L;
	@Inject
	private SessionManager sessionManager;

	private String nextMenu;

	public String getUserId() throws IOException {
		return sessionManager.getLoginUser().getId();
	}

	public String getUserName() throws IOException {
		return sessionManager.getLoginUser().getName();
	}

	public String getWelcomeMsg() throws IOException {
		return "Welcome back, " + sessionManager.getLoginUser().getName();
	}

	public String transfer() {
		return nextMenu + "?faces-redirect=true";
	}

	public boolean hasUserRole(String role) {
		return sessionManager.hasUserRole(role);
	}

	public boolean showPatientRegister() {
		return sessionManager.hasUserRole("admin") || sessionManager.hasUserRole("patient_register");
	}

	public String getNextMenu() {
		return nextMenu;
	}

	public void setNextMenu(String nextMenu) {
		this.nextMenu = nextMenu;
	}

	public String logoff() throws IOException {
		sessionManager.logoff();
		return "/faces/MainMenu.jsf?faces-redirect=true";
	}

	public void handleFileUpload(FileUploadEvent event) {
		System.out.println("fileUploadController");
		FacesMessage msg = new FacesMessage("Succesful", event.getFile()
				.getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void handleFileUpload() {
		System.out.println("fileUploadController");
		FacesMessage msg = new FacesMessage("Succesful", " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

}
